package org.igeek.atomqq.adapter;

import java.util.List;

import org.igeek.atomqq.AtomQQApplication;
import org.igeek.atomqq.activity.BaseActivity;
import org.igeek.atomqq.vo.Chat;
import org.igeek.atomqq.widget.ChatItemView;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

/** 
 * 会话记录适配器 
 * @author <a href="http://hangxin1940.cnblogs.com">hangxin1940@gmail.com</a>
 * @time 2012-2-2 下午05:27:30 
 */
public class ChatsListAdapter extends BaseAdapter{

	private AtomQQApplication application;
	private BaseActivity context;
	
	public ChatsListAdapter(BaseActivity context) {
		application=(AtomQQApplication) context.getApplication();
		this.context=context;
	}
	

	@Override
	public int getCount() {
		return application.getChatCount();
	}



	@Override
	public Object getItem(int position) {
		return application.getChat(position);
	}



	@Override
	public long getItemId(int position) {
		Chat chat=application.getChat(position).get(0);
		
		int type=chat.getType();
		
		if(type==Chat.FRIEND)
			return chat.getUin();
		else if(type==Chat.GROUP)
			return chat.getGcode();
		
		return 0;
	}



	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		List<Chat> chats=application.getChat(position);
		
		for(Chat chat:chats){
			if(chat.getType()==Chat.FRIEND){
				if(0!=chat.getUin()){
					ChatViewHolder holder=new ChatViewHolder(context);
					holder.chatView.setChat(chat);
					return holder.chatView;
				}else{
					continue;
				}
			}else{
				if(0!=chat.getGcode()){
					ChatViewHolder holder=new ChatViewHolder(context);
					holder.chatView.setChat(chat);
					return holder.chatView;
				}else{
					continue;
				}
			}
			
		}
		
		return new View(context);
		
		
	}
	
	
	
	static class ChatViewHolder{
		static ChatItemView chatView;
		public ChatViewHolder(BaseActivity context) {
			chatView=new ChatItemView(context);
		}
	}



}
